// 配置Tailwind自定义颜色和字体
tailwind.config = {
    theme: {
        extend: {
            colors: {
                primary: '#165DFF',
                secondary: '#36CFC9',
                accent: '#722ED1',
                neutral: '#F5F7FA',
                dark: '#1D2129',
                success: '#52C41A',
                warning: '#FAAD14',
                danger: '#F5222D'
            },
            fontFamily: {
                inter: ['Inter', 'system-ui', 'sans-serif'],
            },
        }
    }
}

document.addEventListener('DOMContentLoaded', function() {
    // 验证用户登录状态
    const users = JSON.parse(localStorage.getItem('registeredUsers')) || [];
    const loginEmail = localStorage.getItem('currentLoginEmail');
    
    if (!loginEmail) {
        window.location.href = 'index.html';
        return;
    }
    
    // 获取当前用户信息
    const currentUser = users.find(user => user.email === loginEmail);
    if (currentUser) {
        // 填充用户信息到表单
        document.getElementById('user-display-name').textContent = currentUser.name;
        document.getElementById('full-name').value = currentUser.name || '';
        document.getElementById('nickname').value = currentUser.nickname || '';
        document.getElementById('email').value = currentUser.email || '';
        document.getElementById('phone').value = currentUser.phone || '';
        document.getElementById('bio').value = currentUser.bio || '';
        
        // 更新头像
        if (currentUser.avatar) {
            document.getElementById('profile-pic-preview').src = currentUser.avatar;
            document.getElementById('header-avatar').src = currentUser.avatar;
        }
    } else {
        localStorage.removeItem('currentLoginEmail');
        window.location.href = 'index.html';
    }
    
    // 移动端菜单切换
    const mobileMenuButton = document.getElementById('mobile-menu-button');
    const mobileMenu = document.getElementById('mobile-menu');
    
    mobileMenuButton.addEventListener('click', function() {
        mobileMenu.classList.toggle('hidden');
    });
    
    // 头像上传预览
    const profilePicUpload = document.getElementById('profile-pic-upload');
    const profilePicPreview = document.getElementById('profile-pic-preview');
    const profilePicContainer = profilePicPreview.parentElement;
    
    profilePicContainer.addEventListener('click', function() {
        profilePicUpload.click();
    });
    
    profilePicUpload.addEventListener('change', function(e) {
        if (e.target.files && e.target.files[0]) {
            const reader = new FileReader();
                        
            reader.onload = function(e) {
                profilePicPreview.src = e.target.result;
                document.getElementById('header-avatar').src = e.target.result;
                
                // 保存头像到用户数据
                if (currentUser) {
                    currentUser.avatar = e.target.result;
                    const userIndex = users.findIndex(u => u.email === loginEmail);
                    if (userIndex !== -1) {
                        users[userIndex] = currentUser;
                        localStorage.setItem('registeredUsers', JSON.stringify(users));
                        
                        // 新增：派发头像更新事件
                        const event = new CustomEvent('avatarUpdated', {
                            detail: { avatar: e.target.result }
                        });
                        window.dispatchEvent(event);
                        
                        showToast('头像更新成功');
                    }
                }
            }
            reader.readAsDataURL(e.target.files[0]);
        }
    });
    
    // 个人资料表单提交
    document.getElementById('profile-form').addEventListener('submit', function(e) {
        e.preventDefault();
        
        if (currentUser) {
            // 更新用户信息
            currentUser.name = document.getElementById('full-name').value;
            currentUser.nickname = document.getElementById('nickname').value;
            currentUser.phone = document.getElementById('phone').value;
            currentUser.bio = document.getElementById('bio').value;
            
            // 保存到本地存储
            const userIndex = users.findIndex(u => u.email === loginEmail);
            if (userIndex !== -1) {
                users[userIndex] = currentUser;
                localStorage.setItem('registeredUsers', JSON.stringify(users));
                document.getElementById('user-display-name').textContent = currentUser.name;
                showToast('个人资料更新成功');
            }
        }
    });
    
    // 取消个人资料编辑
    document.getElementById('cancel-profile').addEventListener('click', function() {
        // 重置表单为当前用户数据
        if (currentUser) {
            document.getElementById('full-name').value = currentUser.name || '';
            document.getElementById('nickname').value = currentUser.nickname || '';
            document.getElementById('phone').value = currentUser.phone || '';
            document.getElementById('bio').value = currentUser.bio || '';
        }
    });
    
    // 退出登录
    document.getElementById('logout-btn').addEventListener('click', function() {
        if (confirm('确定要退出登录吗？')) {
            localStorage.removeItem('currentLoginEmail');
            window.location.href = 'index.html';
        }
    });
    
    // 显示提示消息
    function showToast(message) {
        const toast = document.getElementById('success-toast');
        document.getElementById('toast-message').textContent = message;
        
        // 显示toast
        toast.classList.remove('translate-y-20', 'opacity-0');
        toast.classList.add('translate-y-0', 'opacity-100');
        
        // 3秒后隐藏
        setTimeout(() => {
            toast.classList.remove('translate-y-0', 'opacity-100');
            toast.classList.add('translate-y-20', 'opacity-0');
        }, 3000);
    }
     // 用户菜单下拉功能
            const userMenuButton = document.getElementById('user-menu-button');
            const userMenuDropdown = document.getElementById('user-menu-dropdown');
            
            // 点击头像切换菜单显示/隐藏
            userMenuButton.addEventListener('click', function(event) {
                event.stopPropagation(); // 防止事件冒泡
                userMenuDropdown.classList.toggle('hidden');
            });
            
            // 点击页面其他地方关闭菜单
            document.addEventListener('click', function(event) {
                if (!userMenuButton.contains(event.target) && !userMenuDropdown.contains(event.target)) {
                    userMenuDropdown.classList.add('hidden');
                }
            });
            
            // 退出登录功能
            document.getElementById('logout-button').addEventListener('click', function() {
                localStorage.removeItem('currentLoginEmail');
                window.location.href = 'index.html';
            });
            
     updateAvatar();
    
    // 监听头像更新事件
    window.addEventListener('avatarUpdated', function(e) {
        const headerAvatar = document.getElementById('header-avatar');
        if (headerAvatar) {
            headerAvatar.src = e.detail.avatar;
        }
    });
    
    // 监听本地存储变化（应对多标签页情况）
    window.addEventListener('storage', function(e) {
        if (e.key === 'registeredUsers') {
            updateAvatar();
        }
    });
});
